package main

import (
	"fmt"
	"github.com/antchfx/htmlquery"
	"io"
	"net/http"
)

func main() {

	var client = http.Client{}
	baseURL := "https://movie.douban.com/top250?start=%d&filter="

	for start := 0; start < 250; start += 25 {
		url := fmt.Sprintf(baseURL, start)
		//fmt.Println(url)

		req, err := http.NewRequest("GET", url, nil)
		if err != nil {
			panic(err)
		}
		req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")

		resp, err := client.Do(req)
		if err != nil {
			panic(err)
		}
		defer func(Body io.ReadCloser) {
			err := Body.Close()
			if err != nil {
				panic(err)
			}
		}(resp.Body)

		doc, err := htmlquery.Parse(resp.Body)
		if err != nil {
			panic(err)
		} //*[@id="content"]/div/div[1]/ol/li[1]/div/div[2]/div[2]/p[2]/span
		ols := htmlquery.Find(doc, "//div/div[1]/ol/li")
		for _, ol := range ols {
			name := htmlquery.FindOne(ol, "//div/div[2]/div[2]/p[2]/span")

			var text string
			if name != nil {
				text = htmlquery.InnerText(name)
			} else if name == nil {
				text = "  "
			}
			fmt.Println(text)

		}
	}
}
